/**
 * @param {string[]} tokens
 * @return {number}
 */
var evalRPN = function(tokens) {

  function operation(left, right, symbol) {
    // console.log(left, right, symbol);
    switch(symbol) {
      case '+':
        return left + right;
      case '-':
        return left - right;
      case '*':
        return left * right;
      case '/':
        let negative = 0;
        if(left < 0) {
          negative++;
          left = -left;
        }
        if(right < 0) {
          negative++;
          right = -right;
        }
        if(negative % 2 == 0)
          return Math.floor(left / right);
        else
          return -Math.floor(left / right);
    }
  }

  let stack = [];
  for(let i = 0; i < tokens.length; i++) {
    if(parseInt(tokens[i]).toString() === 'NaN') {
      let right = stack.pop(), left = stack.pop();
      stack.push(operation(left, right, tokens[i]));
    } else {
      stack.push(parseInt(tokens[i]));
    }
    // console.log(stack);
  }
  return stack.pop();
};
let tokens = ["2","1","+","3","*"];
console.log(evalRPN(tokens));
tokens = ["4","13","5","/","+"];
console.log(evalRPN(tokens));
tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"];
console.log(evalRPN(tokens));
